package com.huangfuyi.datasource.entity.ddl;

import com.huangfuyi.datasource.enums.EMySQLDataType;
import com.huangfuyi.datasource.exceptions.EDDLExecuteException;

import java.util.List;

/**
 * 数据源的数据定义信息
 * <p>
 * 数据定义：数据源特有的数据逻辑分组结构，
 * 例如MySQL的库、表；kafka的Partition、Topic；ElasticSearch的Index
 */
public class EMySQLCreateTableConf extends EMySQLDataDefineConf {

    private List<ColumnConf> columns;

    @Override
    public String toString() {

        boolean hasPrimary = false;
        StringBuilder sql = new StringBuilder();

        sql.append("create table if not exist ").append(this.getDatabase()).append(".").append(this.getTable());
        sql.append("(");
        for (ColumnConf column : columns) {
            sql.append(column.getColumnName()).append(" ").append(column.getDataType().getTypeStr());
            switch (column.getDataType()) {
                case INT:
                    break;
                case VARCHAR:
                    sql.append("(").append(column.getDataLength()).append(")");

            }

            if (hasPrimary && column.isPrimary()) {
                throw new EDDLExecuteException("MySQL建表信息中指定了两个主键，主键有且只能有1个");
            }

            if (column.isPrimary()) {
                hasPrimary = true;
                sql.append(" primary");
            }

            if (column.isNotNull() && column.isAotuInc()) {
                sql.append(" not null");
            }

            if(column.isAotuInc()){
                sql.append(" auto_increment");
            }

            sql.append(",");
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(")");

        return sql.toString();
    }

    public static class ColumnConf {
        private String columnName;

        private EMySQLDataType dataType;

        private int dataLength;

        private Object defaultValue;

        private boolean primary;

        private boolean unique;

        private boolean notNull;

        private boolean aotuInc;

        private String comment;

        public String getColumnName() {
            return columnName;
        }

        public void setColumnName(String columnName) {
            this.columnName = columnName;
        }

        public EMySQLDataType getDataType() {
            return dataType;
        }

        public void setDataType(EMySQLDataType dataType) {
            this.dataType = dataType;
        }

        public int getDataLength() {
            return dataLength;
        }

        public void setDataLength(int dataLength) {
            this.dataLength = dataLength;
        }

        public Object getDefaultValue() {
            return defaultValue;
        }

        public void setDefaultValue(Object defaultValue) {
            this.defaultValue = defaultValue;
        }

        public boolean isPrimary() {
            return primary;
        }

        public void setPrimary(boolean primary) {
            this.primary = primary;
        }

        public boolean isUnique() {
            return unique;
        }

        public void setUnique(boolean unique) {
            this.unique = unique;
        }

        public boolean isNotNull() {
            return notNull;
        }

        public void setNotNull(boolean notNull) {
            this.notNull = notNull;
        }

        public boolean isAotuInc() {
            return aotuInc;
        }

        public void setAotuInc(boolean aotuInc) {
            this.aotuInc = aotuInc;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }
    }
}
